clear
set type double
set more 1

***************************************
*NOTES
*table 4- vehicle currency
*********************************************************************

capture log using log/Table4_vehicle.log, replace

use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
drop date *I
	rename country cty_code
	drop if missing(valueX)
    	drop if missing(currency)
	drop if cty_code=="EU"
	rename valueX value

merge m:1 cty_code using ../rawdata/iso_cty_and_currency, keep(match master) nogen
replace cty_name="User" if cty_code=="QW"

	drop if currency=="EUR"
	drop if cty_code=="US"
	merge m:1 cty_code using ../data/usd_peg, keep(match master) nogen
	drop if usd_peg==1
	drop if missing(currency_code)
	gen Ix=0 if currency==currency_code
	
*drops vehicle_other	
	replace Ix=1 if missing(Ix)
	drop if Ix==1 & currency~="USD"
	duplicates tag vat cncode cty_code year month, gen(tag)
	keep if tag==0
	
	keep vat cncode cty_code year month Ix
save temp/vehicle_Ix, replace	


use ../data/xcurrencychoice_estdata, clear
	merge m:1 hs6 using ../data/Rauch_hs6_concordance, keep(match master) 
	gen dif=con=="n" if _merge==3
	drop _merge

	drop Ix hs4
	gen hs4=substr(cncode,1,4)
	merge 1:1 vat cncode cty_code year month using temp/vehicle_Ix, keep(match) nogen
	merge m:1 vat cty_code hs4 using ../data/mcompetitor_xcurrency_dollar, keep(match master) nogen
	

*REGRESSIONS
*****************
eststo clear
*col 1 - mphi and ms
	eststo c1: reghdfe Ix mphi_xeu ms lnmemp, absorb(cp date) cluster(vat)

*col 2 - split mphi
	eststo c2: reghdfe Ix mphi_xeu_euro mphi_xeu_other mphi_xeu_missing ms lnmemp , absorb(cp date) cluster(vat)

*col 3: split mphi further into dollar/nodollar
	eststo c3: reghdfe Ix mphi_xeu_euro mphi_xeu_dollar mphi_xeu_nodollar mphi_xeu_missing ms lnmemp, absorb(cp date) cluster(vat)

*col 4: add fdi
	eststo c4: reghdfe Ix mphi_xeu_euro mphi_xeu_dollar mphi_xeu_nodollar mphi_xeu_missing ms lnmemp inFDI, absorb(cp date) cluster(vat)

*col 5:  with mshcomp_dollar - iv
	eststo c5: ivreghdfe Ix mphi_xeu_euro mphi_xeu_dollar mphi_xeu_nodollar mphi_xeu_missing ms lnmemp (mshcompetitor_dollar = IVsh_US IVsh_CHN IVsh_other lncomp_memp comp_mphi_xeu comp_mphi_xeu_dollar) , absorb(cty_num hs4 date) cluster(vat) first savefirst savefprefix(c5_fs_)
	
		
*col 6: as col3 but only dif
	eststo c6: reghdfe Ix mphi_xeu_euro mphi_xeu_dollar mphi_xeu_nodollar mphi_xeu_missing ms lnmemp if dif==1, absorb(cp date) cluster(vat)

*col 7: as col5 but only dif
	eststo c7: ivreghdfe Ix mphi_xeu_euro mphi_xeu_dollar mphi_xeu_nodollar mphi_xeu_missing ms lnmemp (mshcompetitor_dollar = IVsh_US IVsh_CHN IVsh_other lncomp_memp comp_mphi_xeu comp_mphi_xeu_dollar) if dif==1, absorb(cty_num hs4 date) cluster(vat) first savefirst savefprefix(c7_fs_)


* Export regressions
esttab c? using "../results/Table4_vehicle.csv", se  b(3) se(3) r2 ar2 star(* 0.10 ** 0.05 *** 0.01) scalars("cdf Cragg-Donald F stat" "rkf Kleibergen-Paap F stat" "j Hansen J stat" "jp Hansen J stat p-val") compress replace

* Export first-stage regressions 
esttab c?_fs_* using "../results/Table4_vehicle_fs.csv", nodepvars se b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) compress replace
eststo clear
exit

	
